home *** CD-ROM | disk | FTP | other *** search
- Path: hydro.com!usenet
- From: Terje Mathisen <Terje.Mathisen@hda.hydro.com>
- Newsgroups: comp.lang.c,comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.msdos.programmer,comp.programming,comp.windows.ms.programmer.misc
- Subject: Re: Date Arithmetic
- Date: Thu, 22 Feb 1996 13:48:48 +0100
- Organization: Hydro
- Message-ID: <312C6630.916@hda.hydro.com>
- References: <4g19kp$640@tracy.protocom.com> <jrs.2754.000A7444@dclf.npl.co.uk> <4ggsj3$1fg@dopey.magg.net>
- NNTP-Posting-Host: vkhdpc40.hda.hydro.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
- CC: "Michael J. Karas" <mkaras@pclink.com>
-
- Dennis Hawkins wrote:
- >
- > jrs@dclf.npl.co.uk (Dr John Stockton NPL UK) wrote:
- >
- > >In article <4g19kp$640@tracy.protocom.com> "Michael J. Karas" <mkaras@pclink.com> writes:
- >
- > >>I am working on an algorithm for a laser marking machine that writes
- > >>expiration delays on to food product boxes. The algorithm needs to
- > >>be able to add NNN days to todays date in the fastest manner possible
- > >>without using any floating point arithmetic. I could use help from anyone
- >
- > This shouldn't be a big deal. There are several algorithims to handle
- > this. The best one depends on the range of NNN days. If it is just a
- > couple of weeks then add it by brute force to the month. If NNN can
- > go to 999 then you are probably better off converting todays date into
- > a julian or an integer that represents the number of days since your
- > machine was put on line. Then just add the days like you would any
- > other integer. Then convert it back again. Dates can be tricky, you
- > just need to do a lot of testing to make sure they work ok though.
-
- I just wrote a very simple function to do this, it uses no multiplication or
- division operations, just simple logic that your laser marker should have:
-
- On both a Pentium and a PPro cpu, it used less than 90 cycles when compiled with
- Watcom C10.5, full optimization. I tested it by calling the function with today
- (1996-02-21) as the starting point, and for all offsets from 0 to 34999 days ahead.
-
- The last day (today + 34999) seems to be 2091-12-19.
-
- I hope you can use this!
-
- /*
- add_days_to_date() (c) Terje Mathisen 1996.
-
- Assumes all inputs to be OK, the year must be in 1901 to 2099
- range,but no checking is done, to keep the function as fast &
- simple as possible!
-
- */
- int add_days_to_date(int *year, int *month, int *day, int days_to_add)
- {
- static int days_before_month[13] =
- {0,31,61,92,122,153,184,214,245,275,306,337,366};
- int y = *year, m = *month - 3, d = *day - 1;
-
- if (m < 0) {
- m += 12;
- y --;
- }
- /* Add the day number in the current year, day 0 at 1 March! */
- days_to_add += days_before_month[m] + d;
-
- /* Subtract full four-year periods: */
- while (days_to_add >= 1461) { /* Days in a 4-year period, inc. leap day */
- days_to_add -= 1461;
- y += 4;
- }
-
- /* Subtract full years: */
- while (days_to_add >= 365) {
- days_to_add -= 365;
- if (y & 3 == 3) { /* Leap year rule with offset years */
- days_to_add--;
- if (days_to_add < 0) {
- days_to_add = 365; /* Must be 29 Feb */
- break;
- }
- }
- y++;
- }
-
- /* Locate the month with a table lookup, start with a (good)
- approximate value::
- */
- m = days_to_add >> 5;
- if (days_to_add >= days_before_month[m+1]) m++;
- *day = days_to_add - days_before_month[m] + 1;
- if (m > 9) {
- m -= 12;
- y++;
- }
- *year = y;
- *month = m + 3;
- return 0;
- }
-
-
-
-
- --
- -Terje Mathisen (include std disclaimer) <Terje.Mathisen@hda.hydro.com>
- "almost all programming can be viewed as an exercise in caching"
-